package cn.servlet.admin;

import cn.dao.AdminDao;
import cn.dao.CustomerDao;
import cn.pojo.Customer;
import cn.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import com.alibaba.fastjson.JSON;
/**
 * @author jia
 * @Description
 * @date 2019-03-21-16:49
 */
@WebServlet("/userManage")
public class UserManageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CustomerDao customerDao = new CustomerDao();
        // 取出传递参数,并创建分页对象
        int start = Integer.parseInt(req.getParameter("start"));
        int count = Integer.parseInt(req.getParameter("count"));
        // 创建分页对象
        Page page = new Page(start, count);
        page.setTotal(customerDao.getTotal());
        System.out.println(page.toString());
        // 查找数据
        List<Customer> customerList = customerDao.list(page.getStart(), page.getCount());
        // 转换json
        StringBuilder json = new StringBuilder(JSON.toJSONString(customerList));
        // 把分页对象添加到json里
        json.insert(1, "{\"totalPage\"  :"+page.getTotalPage()+",\"total\":"+customerDao.getTotal()+"},");
        System.out.println(json);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        pw.print(json);
        pw.flush();
        pw.close();
    }
}